<?php

namespace Admin\Controller;

/**
* 用户控制器
*/
class UserController extends CommonController
{
    /*用户列表*/
    public function company_user_list()
    {
        $list = $this->lists('User', 'status = 1 and level_id = 1', 'user_id asc');
        $this->assign('list', $list);
        $this->display('company_user_list');
    }

    /*添加公司级别用户*/
    public function add_company_user()
    {
        if (IS_POST) {
            $post = I('post.');
            $post['password'] = md5($post['password']);
            $post['object_ids'] = implode(',', $post['object_ids']);
            $res = M('User')->add($post);
            if (empty($res)) {
                $this->error('fail', U('User/add_company_user'));
            }
            $this->success('success', U('User/company_user_list'));
        }
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->display('add_company_user');
    }

    public function edit_company_user()
    {
        if (IS_POST) {
            $post = I('post.');
            if ($post['password'] != '') {
                $post['password'] = md5($post['password']);
            } else {
                unset($post['password']);
            }
            $post['object_ids'] = implode(',', $post['object_ids']);
            $res = M('User')->where(['user_id' => $post['user_id']])->save($post);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('User/company_user_list'));
        }
        $user_id = I('user_id');
        $info = M('User')->where(['user_id' => $user_id])->find();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('info', $info);
        $this->display('edit_company_user');
    }

    public function del_company_user()
    {
        $user_id = I('user_id');
        M('User')->where(['user_id' => $user_id])->setField('status', 0);
        $this->success('删除成功', U('User/company_user_list'));
    }

    /*用户列表*/
    public function part_user_list()
    {
        $list = $this->lists('User', 'status = 1 and level_id = 2', 'user_id asc');
        $this->assign('list', $list);
        $this->display('part_user_list');
    }

    /*添加部门级别用户*/
    public function add_part_user()
    {
        if (IS_POST) {
            $post = I('post.');
            $post['object_ids'] = implode(',', $post['object_ids']);
            $post['password'] = md5($post['password']);
            $res = M('User')->add($post);
            if (empty($res)) {
                $this->error('fial', U('User/add_part_user'));
            }
            $this->success('success', U('User/part_user_list'));
        }
        $parent = M('User')->where(['status'=>1,'level_id'=>1])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->display('add_part_user');
    }

    public function edit_part_user()
    {
        if (IS_POST) {
            $post = I('post.');
            if ($post['password'] != '') {
                $post['password'] = md5($post['password']);
            } else {
                unset($post['password']);
            }
            $post['object_ids'] = implode(',', $post['object_ids']);
            $res = M('User')->where(['user_id' => $post['user_id']])->save($post);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('User/part_user_list'));
        }
        $user_id = I('user_id');
        $info = M('User')->where(['user_id' => $user_id])->find();
        $parent = M('User')->where(['status'=>1,'level_id'=>1])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->assign('info', $info);
        $this->display('edit_part_user');
    }

    public function del_part_user()
    {
        $user_id = I('user_id');
        M('User')->where(['user_id' => $user_id])->setField('status', 0);
        $this->success('删除成功', U('User/part_user_list'));
    }

    /*用户列表*/
    public function workshop_user_list()
    {
        $list = $this->lists('User', 'status = 1 and level_id = 3', 'user_id asc');
        $this->assign('list', $list);
        $this->display('workshop_user_list');
    }

    /*添加部门级别用户*/
    public function add_workshop_user()
    {
        if (IS_POST) {
            $post = I('post.');
            $post['object_ids'] = implode(',', $post['object_ids']);
            $post['password'] = md5($post['password']);
            $res = M('User')->add($post);
            if (empty($res)) {
                $this->error('fial', U('User/add_workshop_user'));
            }
            $this->success('success', U('User/workshop_user_list'));
        }
        $parent = M('User')->where(['status'=>1,'level_id'=>2])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->display('add_workshop_user');
    }

    public function edit_workshop_user()
    {
        if (IS_POST) {
            $post = I('post.');
            if ($post['password'] != '') {
                $post['password'] = md5($post['password']);
            } else {
                unset($post['password']);
            }
            $post['object_ids'] = implode(',', $post['object_ids']);
            $res = M('User')->where(['user_id' => $post['user_id']])->save($post);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('User/workshop_user_list'));
        }
        $user_id = I('user_id');
        $info = M('User')->where(['user_id' => $user_id])->find();
        $parent = M('User')->where(['status'=>1,'level_id'=>2])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->assign('info', $info);
        $this->display('edit_workshop_user');
    }

    public function del_workshop_user()
    {
        $user_id = I('user_id');
        M('User')->where(['user_id' => $user_id])->setField('status', 0);
        $this->success('删除成功', U('User/workshop_user_list'));
    }

    /*用户列表*/
    public function group_user_list()
    {
        $list = $this->lists('User', 'status = 1 and level_id = 4', 'user_id asc');
        $this->assign('list', $list);

        $Data = M('User'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1 and level_id = 4')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出


        $this->display('group_user_list');



    }

    /*添加部门级别用户*/
    public function add_group_user()
    {
        if (IS_POST) {
            $post = I('post.');
            $post['object_ids'] = implode(',', $post['object_ids']);
            $post['password'] = md5($post['password']);
            $res = M('User')->add($post);
            if (empty($res)) {
                $this->error('fial', U('User/add_group_user'));
            }
            $this->success('success', U('User/group_user_list'));
        }
        $parent = M('User')->where(['status'=>1,'level_id'=>3])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->display('add_group_user');
    }

    public function edit_group_user()
    {
        if (IS_POST) {
            $post = I('post.');
            if ($post['password'] != '') {
                $post['password'] = md5($post['password']);
            } else {
                unset($post['password']);
            }
            $post['object_ids'] = implode(',', $post['object_ids']);
            $res = M('User')->where(['user_id' => $post['user_id']])->save($post);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('User/group_user_list'));
        }
        $user_id = I('user_id');
        $info = M('User')->where(['user_id' => $user_id])->find();
        $parent = M('User')->where(['status'=>1,'level_id'=>3])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->assign('info', $info);
        $this->display('edit_group_user');
    }

    public function del_group_user()
    {
        $user_id = I('user_id');
        M('User')->where(['user_id' => $user_id])->setField('status', 0);
        $this->success('删除成功', U('User/group_user_list'));
    }

    /*用户列表*/
    public function user_list()
    {
        $list = $this->lists('User', 'status = 1 and level_id = 5', 'user_id asc');
        $this->assign('list', $list);

        $Data = M('User'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1 and level_id = 5')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display('user_list');

    }

    /*添加部门级别用户*/
    public function add_user()
    {
        if (IS_POST) {
            $post = I('post.');
            $post['object_ids'] = implode(',', $post['object_ids']);
            $post['password'] = md5($post['password']);
            $res = M('User')->add($post);
            if (empty($res)) {
                $this->error('fial', U('User/add_user'));
            }
            $this->success('success', U('User/user_list'));
        }
        $parent = M('User')->where(['status'=>1,'level_id'=>4])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->display('add_user');
    }

    public function add_csv()
    {
        if (IS_POST) {
            if (isset($_FILES['user'])) {
                $file = fopen($_FILES["user"]["tmp_name"], 'r');
                $data = input_csv($file);
                print_r($data);die;
            }
        }
        $this->display('add_csv');
    }

    public function edit_user()
    {
        if (IS_POST) {
            $post = I('post.');
            if ($post['password'] != '') {
                $post['password'] = md5($post['password']);
            } else {
                unset($post['password']);
            }
            $post['object_ids'] = implode(',', $post['object_ids']);
            $res = M('User')->where(['user_id' => $post['user_id']])->save($post);
            if (empty($res)) {
                $this->error('保存数据出错。');
            }
            $this->success('保存成功。', U('User/user_list'));
        }
        $user_id = I('user_id');
        $info = M('User')->where(['user_id' => $user_id])->find();
        $parent = M('User')->where(['status'=>1,'level_id'=>4])->select();
        $object = M('Object')->where(['status' => 1])->select();
        foreach ($object as $k => $v) {
            $object[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            $object[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
        }
        $this->assign('object', $object);
        $this->assign('parent', $parent);
        $this->assign('info', $info);
        $this->display('edit_user');
    }

    public function del_user()
    {
        $user_id = I('user_id');
        M('User')->where(['user_id' => $user_id])->setField('status', 0);
        $this->success('删除成功', U('User/user_list'));
    }
}
